<template>
  <div>
    <!-- 卡片试图区域 -->
    <el-card class="box-card">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="关键字" style="font-weight:700">
          <el-input v-model="formInline.keyword" placeholder="根据文章标题搜索"></el-input>
        </el-form-item>
        <el-form-item label="状态" style="font-weight:700">
          <el-select v-model="formInline.state" placeholder="请选择">
            <el-option label="启用" :value="0"></el-option>
            <el-option label="禁用" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="clear">清除</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">搜索</el-button>
        </el-form-item>
        <el-button
          type="success"
          icon="el-icon-edit"
          style="float:right"
          @click="showNewTechnique=true"
        >新增技巧</el-button>
      </el-form>
      <el-alert
        :title="`数据一共${counts}条`"
        type="info"
        show-icon
        :closable="false"
        style="margin-bottom:15px"
      ></el-alert>
      <!-- 文章列表区域 -->
      <el-table
        :data="articlelist"
        :header-cell-style="{background:'#fafafa',color:'#909399',borderBottom:'2px solid #e8e8e8'}"
        style="width: 100%"
      >
        <el-table-column prop="id" label="序号" width="80"></el-table-column>
        <el-table-column prop="title" label="文章标题" width="380"></el-table-column>
        <el-table-column prop="visits" label="阅读数"></el-table-column>
        <el-table-column prop="username" label="录入人"></el-table-column>
        <el-table-column prop="createTime" label="录入时间">
          <template v-slot="slotProps">{{slotProps.row.createTime | dateFormat}}</template>
        </el-table-column>
        <el-table-column prop="state" label="状态">
          <template v-slot="slotProps">
            <div>{{slotProps.row.state === 1?'已启用':'已禁用'}}</div>
            <!-- <p v-if="slotProps.row.state === 1">已启用</p>
            <p v-else>已禁用</p>-->
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150">
          <template v-slot="slotProps">
            <el-link type="primary" :underline="false" @click="previewArt(slotProps.row)">预览</el-link>
            <el-link
              type="primary"
              :underline="false"
              @click="stateArticle(slotProps.row)"
            >{{slotProps.row.state === 1?'启用':'禁用'}}</el-link>
            <el-link
              type="primary"
              :underline="false"
              @click="editArt(slotProps.row)"
              :disabled="slotProps.row.state===1"
            >修改</el-link>
            <el-link
              type="primary"
              :underline="false"
              @click="removeArticle(slotProps.row.id)"
              :disabled="slotProps.row.state===1||slotProps.row.totals > 0"
            >删除</el-link>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        class="paginationList"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="formInline.page"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="formInline.pagesize"
        background
        layout="prev, pager, next,sizes, jumper"
        :total="counts"
      ></el-pagination>
    </el-card>
    <!-- 新增技巧的对话框 -->
    <el-dialog title="新增文章" :visible.sync="showNewTechnique" width="60%" @close="addDialogClosed">
      <!-- 内容主体区域 -->
      <span>
        <!-- 文章标题 -->
        <el-form :model="addForm" :rules="addFormRules" ref="ruleFormRef" label-width="100px">
          <el-form-item label="文章标题:" prop="title">
            <el-input v-model="addForm.title"></el-input>
          </el-form-item>
          <el-form-item label="文章内容:" prop="articleBody">
            <!-- 富文本编辑器组件 -->
            <quill-editor v-model="addForm.articleBody"></quill-editor>
          </el-form-item>
          <el-form-item label="视频地址:" prop="videoURL">
            <el-input v-model="addForm.videoURL"></el-input>
          </el-form-item>
        </el-form>
      </span>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="showNewTechnique = false">取 消</el-button>
        <el-button type="primary" @click="addList">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 展示预览文章的对话框 -->
    <el-dialog title="预览文章" :visible.sync="previewDialogVisible" width="60%">
      <!-- 主体区域 -->
      <div class="preview-box">
        <div class="preview-title">{{ articlesInfo.title }}</div>
        <div class="preview-nav">
          <span>{{ articlesInfo.createTime }}</span>
          <span>{{ articlesInfo.username }}</span>
          <span class="el-icon-view"></span>
          <span>{{ articlesInfo.visits }}</span>
        </div>
        <div class="preview-content" v-html="articlesInfo.articleBody"></div>
      </div>
    </el-dialog>
    <!-- 展示修改文章的对话框 -->
    <el-dialog title="修改文章" :visible.sync="editDialogVisible" width="60%" @close="resetEditArt">
      <!-- 表单区域 -->
      <el-form
        :model="editArticlesForm"
        :rules="articlesFormRules"
        ref="editArticlesFormRef"
        label-width="80px"
        class="demo-ruleForm"
      >
        <el-form-item label="文章标题" prop="title" class="elform">
          <el-input v-model="editArticlesForm.title" placeholder="请输入文章标签"></el-input>
        </el-form-item>
        <el-form-item label="文章标题" prop="articleBody" class="elform">
          <quill-editor
            style="margin-left: 0px;"
            v-model="editArticlesForm.articleBody"
            ref="myQuillEditor"
          ></quill-editor>
        </el-form-item>
        <el-form-item label="视频地址" class="elform">
          <el-input v-model="editArticlesForm.videoURL" placeholder="请输入视频地址"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editArticlesFrom">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  getArticleList,
  addArticle,
  removeArticleById,
  articleState,
  updateArticle
} from '@/api/article.js'
export default {
  name: 'articlesList',
  components: {},
  props: {},
  data() {
    return {
      formInline: {
        page: 1,
        pagesize: 2,
        keyword: '',
        state: null
      },
      articlelist: [],
      // 总条数
      counts: 0,
      // 控制新增技巧对话框的显示与隐藏
      showNewTechnique: false,
      // 控制预览文章对话框的显示与隐藏
      // previewArticle: false,
      previewDialogVisible: false,
      // 控制修改文章对话框的显示与隐藏
      editArticle: false,
      // 添加文章表单数据
      addForm: {
        title: '',
        articleBody: '',
        videoURL: ''
      },
      // 添加表单验证规则对象
      addFormRules: {
        title: [{ required: true, message: '请输入文章标题', trigger: 'blur' }],
        articleBody: [
          { required: true, message: '请输入文章内容', trigger: 'blur' }
        ],
        videoURL: [
          { required: true, message: '请输入文章内容', trigger: 'blur' }
        ]
      },
      // 修改文章的信息对象
      // editArticlesFrom: {
      //   title: '',
      //   articleBody: '',
      //   videoURL: null,
      //   id: ''
      // },
      // 点击当前预览的数据
      articlesInfo: [],
      // 修改弹框的显示
      editDialogVisible: false,
      // 修改表的数据
      editArticlesForm: {
        id: '',
        title: '',
        articleBody: '',
        videoURL: null
      },
      // 添加验证
      articlesFormRules: {
        title: [{ required: true, message: '请输入活动名称', trigger: 'blur' }],
        articleBody: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ]
      },
      // 添加框数据
      articlesForm: {
        title: '',
        articleBody: '',
        videoURL: null
      }
    }
  },
  computed: {},
  created() {
    this.getarticleList()
  },
  mounted() {},
  methods: {
    onSubmit() {
      // console.log('submit!')
      // this.queryinfo.pagesize = newsize
      // this.queryinfo.page = newPage
      this.formInline.keyword = ''
      this.formInline.state = null
      this.getarticleList()
    },
    async getarticleList() {
      try {
        const { data } = await getArticleList(this.queryinfo)
        console.log(data)
        this.articlelist = data.items
        // console.log(this.articlelist)

        this.counts = data.counts
      } catch (err) {
        this.$toast.fail('获取文章列表失败')
      }
    },
    // 监听 pagesize 改变的事件
    handleSizeChange(newsize) {
      // console.log(newsize)
      this.formInline.pagesize = newsize
      this.getarticleList()
    },
    // 监听 页码值 改变事件
    handleCurrentChange(newPage) {
      // console.log(newPage)
      this.formInline.page = newPage
      this.getarticleList()
    },
    // 监听添加文章数据对话框关闭事件
    addDialogClosed() {
      this.$refs.ruleFormRef.resetFields()
    },
    // 点击按钮, 添加新数据
    addList() {
      this.$refs.ruleFormRef.validate(async valid => {
        // console.log(valid)
        if (!valid) return
        // 可以发起添加文章的网络请求
        const { data: res } = await addArticle(this.addForm)
        console.log(res)
        this.$message.success('添加文章数据成功')
        this.showNewTechnique = false
        this.getarticleList()
      })
    },
    // 点击清除,清空关键字框和状态框
    clear() {
      this.formInline.keyword = ''
      this.formInline.state = null
      this.getarticleList()
    },
    // 展示预览的对话框
    // showBox() {
    //   this.previewArticle = true
    // },
    // 展示修改文章的对话框
    async reviseArticle(id) {
      // console.log(id)
      this.editArticle = true
    },
    async removeArticle(id) {
      // 弹框询问用户文章数据是否删除
      const confirmResult = await this.$confirm(
        '此操作将永久删除该文章, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)
      // 如果用户确认删除,则返回值为字符串 confirm
      // console.log(confirmResult)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      // await this.$http.DELETE('/articles/' + id)
      // this.$message.success('删除文章成功!')
      // this.getarticleList()
      await removeArticleById(id)
      // this.getarticleList
      this.$message.success('删除文章成功!')
      this.getarticleList()
    },
    async stateArticle(data) {
      await articleState({
        id: data.id,
        state: data.state === 0 ? 1 : 0
      })
      data.state = data.state === 0 ? 1 : 0
      this.$message.success('操作成功')
    },
    // 点击预览
    previewArt(row) {
      this.articlesInfo = row
      // console.log(this.articlesInfo)
      this.previewDialogVisible = true
    },
    // 修改的重置
    resetEditArt() {
      this.$refs.editArticlesFormRef.resetFields()
    },
    // 点击修改弹框显示
    editArt(row) {
      // console.log(row)
      this.editArticlesForm.title = row.title
      this.editArticlesForm.articleBody = row.articleBody
      this.editArticlesForm.videoURL = row.videoURL
      this.editArticlesForm.id = row.id
      this.editDialogVisible = true
    },

    // 点击修改请求
    async editArticlesFrom() {
      try {
        await updateArticle.update(this.editArticlesForm)
        this.editDialogVisible = false
        this.getarticleList()
        this.$message.success('修改文章成功')
      } catch (err) {
        this.$message.success('修改文章失败')
      }
    }
    // 重置添加表单
    // resetAddArt() {
    //   this.$refs.articlesFormRef.resetFields()
    //   this.articlesForm.articleBody = ''
    //   this.articlesForm.videoURL = ''
    // }
  }
}
</script>
<style lang="less" scoped>
.paginationList {
  margin-top: 20px;
  text-align: right;
}
a {
  margin: 0 2px;
  color: #6b9eff;
}
.el-form-item {
  font-weight: 700;
}
</style>
